//
//  MyViewController.m
//  weibo
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import "MyModel.h"
#import "MyTableViewCell.h"

//#import "MyLoginViewController.h"

#import <EaseMobSDKFull/EaseMob.h>

static NSString *const mineReuse = @"mineReuse";


@interface MyViewController ()

@property (nonatomic, strong) UITableView *myTabelView;
@property (nonatomic, strong) UIImageView *backImageV;
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *userPic;
@property (nonatomic, copy)NSString *userNames;
@property (nonatomic, copy)NSString *password;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 70) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"MyTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:mineReuse];
    
    //添加头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, 185)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 70)];
    footerView.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(1.0 / 6 * WIDTH, 10, 2.0 / 3 * WIDTH, 50);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:button];
//    self.myTabelView.tableHeaderView = headerView;
    self.myTabelView.tableFooterView = footerView;
//    [headerView release];
    [footerView release];
    NSLog(@"%@",footerView.subviews);
//    [self addHeader];
    
    [self createView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"点击" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];

//    [[UIApplication sharedApplication] openURL:@"tel://110"];

   


}

- (void)btnAction:(UIButton *)button {
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        if (!error && info) {
            NSLog(@"退出成功");
        }
    } onQueue:nil];
}


- (void)createView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -300, 100, 100)];
    [self.view addSubview:view];
    view.tag = 10000;
    view.backgroundColor = [UIColor redColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 100, 50);
    [button setTitle:@"注册" forState:UIControlStateNormal];
    button.tag = 10001;
    button.backgroundColor = [UIColor blueColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *Lbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    Lbutton.frame = CGRectMake(0, 50, 100, 50);
    [Lbutton setTitle:@"登录" forState:UIControlStateNormal];
    Lbutton.tag = 10002;
    Lbutton.backgroundColor = [UIColor cyanColor];
    [Lbutton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    [view addSubview:Lbutton];
    
}

- (void)rightBarAction:(UIBarButtonItem *)item{
    NSLog(@"asjkfdsf");


}



- (void)buttonAction:(UIButton *)button {
    if (button.tag == 10001) {
        [self registerUser];
    } else {
        [self loading];
    }
}

- (void)registerUser {
//    NSLog(@"注册成功");
//    __block NSString *nameStr = nil;
//    __block NSString *passwordStr = nil;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"注册" message:@"请注册" preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//        self.userNames = textField.text;
//        NSLog(@"%@",textField.text);
        [textField addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
        textField.tag = 11000;
        textField.placeholder = @"请输入用户";
        
    }];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
//        self.password = textField.text;
        [textField addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
        textField.tag = 11001;
        textField.placeholder = @"请输入密码";
        textField.secureTextEntry = YES;
        
    }];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"zhuce" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
             [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userNames password:self.password withCompletion:^(NSString *username, NSString *password, EMError *error) {
                if (!error) {
                    NSLog(@"注册成功");
                    
                } else {
                    NSLog(@"%@",error);
                }
                
            } onQueue:nil];
            NSLog(@"............%@",self.userNames);
            NSLog(@"%@",self.password);
                if (self.password) {

                    NSLog(@"注册成功");
                }else{
//                    NSLog(@"==========%@",error);
                };
        
    }]];
    [self presentViewController:alertVC animated:YES completion:^{
        
        
    }];
    
}

- (void)textAction:(UITextField *)textField {
    if (textField.tag == 11000) {
        self.userNames = textField.text;
    } else if(textField.tag == 11001) {
        self.password = textField.text;
    } else if (textField.tag == 11002) {
        self.userNames = textField.text;
    } else if (textField.tag == 11003) {
        self.password = textField.text;
    }
    
}

- (void)loading {
//    NSLog(@"登录成功");
    self.password = nil;
    self.userNames = nil;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登录" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //        self.userNames = textField.text;
        //        NSLog(@"%@",textField.text);
        [textField addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
        textField.tag = 11002;
        textField.placeholder = @"请输入用户";
        
    }];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
        //        self.password = textField.text;
        [textField addTarget:self action:@selector(textAction:) forControlEvents:UIControlEventEditingChanged];
        textField.tag = 11003;
        textField.placeholder = @"请输入密码";
        textField.secureTextEntry = YES;
        
    }];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"zhuce" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIView *view = [self.view viewWithTag:10000];
        view.frame = CGRectMake(0, -300, 100, 100);
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userNames password:self.password completion:^(NSDictionary *loginInfo, EMError *error) {
            if (!error) {
                NSLog(@"%@",loginInfo);
                NSLog(@"登录成功");
                [[[EaseMob sharedInstance] chatManager] setIsAutoLoginEnabled:YES];
//                UIView *view = [self.view viewWithTag:10000];
//                view.frame = CGRectMake(0, -300, 100, 100);
            } else {
                NSLog(@"%@",error);
            }
            
        } onQueue:nil];
        
        
        
        
    }]];
    [self presentViewController:alertVC animated:YES completion:^{
        
        
    }];

    
}

- (void)barAction:(UIBarButtonItem *)bar {
    UIView *view = [self.view viewWithTag:10000];
    [UIView animateWithDuration:0.2 animations:^{
        view.frame = CGRectMake(WIDTH - 100, 0, 100, 100);
        
    } completion:^(BOOL finished) {
        
        
    }];
}

//数据
- (void)getData
{
    self.myArrayM = [NSMutableArray array];
    self.myArrayMA = [NSMutableArray array];
    self.myArrayMB = [NSMutableArray array];
    self.myArrayMC = [NSMutableArray array];
    self.myArrayMD = [NSMutableArray array];
    self.myArrayME = [NSMutableArray array];
    self.myArrayMF = [NSMutableArray array];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"朋友圈", @"新的朋友", @"扫一扫",@"二维码", @"我的赞", @"微博支付",@"微博运动", @"粉丝头条", @"粉丝服务", @"草稿箱",@"更多", nil];
    for (int i = 0; i < 11; i++) {
        MyModel *model = [[MyModel alloc] init];
        model.title = [array objectAtIndex:i];
        model.photo = [NSString  stringWithFormat:@"btn_mine_icon%d",i+1];
        if (i < 1) {
            [self.myArrayMA addObject:model];
        }else if (i >= 1 && i < 2) {
            [self.myArrayMB addObject:model];
        }else if (i >= 2 && i < 4) {
            [self.myArrayMC addObject:model];
        }
//        }else if (i >= 7 && i < 9) {
//            [self.myArrayMD addObject:model];
//        }else if (i == 9) {
//            [self.myArrayME addObject:model];
//        }else {
//            [self.myArrayMF addObject:model];
//        }
    }
    [self.myArrayM addObject:self.myArrayMA];
    [self.myArrayM addObject:self.myArrayMB];
    [self.myArrayM addObject:self.myArrayMC];
    [self.myArrayM addObject:self.myArrayMD];
    [self.myArrayM addObject:self.myArrayME];
    [self.myArrayM addObject:self.myArrayMF];

}


//addHearder
- (void)addHeader
{

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 10)];
    view.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1.0];
    [self.myTabelView addSubview:view];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    view2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1.0];
    [self.myTabelView addSubview:view2];
    
    //白色透明条
    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(0, 115, WIDTH, 70)];
    backV.backgroundColor = [UIColor clearColor];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 115, WIDTH, 70)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.alpha = 0.3;
    [self.myTabelView addSubview:toolbar];
    
    //loginButton
//    [self addHeaderLoginButton];
    
    //添加按钮
    [self addButton:@"微博" Number:1];
    [self addButton:@"关注" Number:2];
    [self addButton:@"粉丝" Number:3];
}

-(void)addButton:(NSString *)labelName Number:(NSInteger )number {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0 + WIDTH / 3 * (number - 1), 115, WIDTH / 3, 25)];
    label.backgroundColor = [UIColor clearColor];
    label.text = labelName;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    [self.myTabelView addSubview:label];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.frame = CGRectMake(0 +  WIDTH / 3 * (number - 1), 115, WIDTH / 3 , 110);
    button.tag = 300000 + number;
    [button addTarget:self action:@selector(buttonAc) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAc
{
    
}

//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.myArrayM.count;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:235 / 255.0 blue:235 / 255.0 alpha:1.0];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    view2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1.0];
    [self.myTabelView addSubview:view2];
    
    return view;
    
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.myArrayMA.count;
    }
    if (section == 1) {
        return  self.myArrayMB.count;
    }
    if (section == 2) {
        return  self.myArrayMC.count;
    }
    if (section == 3) {
        return  self.myArrayMD.count;
    }
    if (section == 4) {
        return  self.myArrayME.count;
    }
    return self.myArrayMF.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineReuse];
    if (indexPath.section == 0) {
        MyModel *model = [self.myArrayMA objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 1) {
        MyModel *model = [self.myArrayMB objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 2) {
        MyModel *model = [self.myArrayMC objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 3) {
        MyModel *model = [self.myArrayMD objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 4) {
        MyModel *model = [self.myArrayME objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MyModel *model = [self.myArrayMF objectAtIndex:indexPath.row];
    cell.myModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
